﻿CREATE PROCEDURE [dbo].[yaf_user_upgrade](
                @UserID INT)
AS
    BEGIN
        DECLARE  @RankID INT
        DECLARE  @Flags INT
        DECLARE  @MinPosts INT
        DECLARE  @NumPosts INT
        -- Get user and rank information
        SELECT @RankID = b.RankID,
               @Flags = b.Flags,
               @MinPosts = b.MinPosts,
               @NumPosts = a.NumPosts
        FROM   yaf_User a,
               yaf_Rank b
        WHERE  a.UserID = @UserID
        AND b.RankID = a.RankID
        -- If user isn't member of a ladder rank, exit
        IF (@Flags & 2) = 0
        RETURN
            -- See if user got enough posts for next ladder group
            SELECT TOP 1 @RankID = RankID
            FROM     yaf_Rank
            WHERE    (Flags & 2) = 2
            AND MinPosts <= @NumPosts
            AND MinPosts > @MinPosts
            ORDER BY MinPosts
        IF @@ROWCOUNT = 1
        UPDATE yaf_User
        SET    RankID = @RankID
        WHERE  UserID = @UserID
    END


